package org.jivesoftware.smackx.provider;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.VCard;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class VCardProvider
  implements IQProvider
{
  private static final String PREFERRED_ENCODING = "UTF-8";

  public IQ parseIQ(XmlPullParser paramXmlPullParser)
    throws Exception
  {
    StringBuilder localStringBuilder = new StringBuilder();
    try
    {
      for (int i = paramXmlPullParser.getEventType(); ; i = paramXmlPullParser.next())
      {
        switch (i)
        {
        case 4:
          localStringBuilder.append(StringUtils.escapeForXML(paramXmlPullParser.getText()));
          break;
        case 2:
          localStringBuilder.append('<').append(paramXmlPullParser.getName()).append('>');
          break;
        case 3:
          localStringBuilder.append("</").append(paramXmlPullParser.getName()).append('>');
        }
        if ((i == 3) && ("vCard".equals(paramXmlPullParser.getName())))
          break;
      }
    }
    catch (XmlPullParserException localXmlPullParserException)
    {
      localXmlPullParserException.printStackTrace();
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
    }
    String str = localStringBuilder.toString();
    return createVCardFromXML(str);
  }

  public static VCard createVCardFromXML(String paramString)
    throws Exception
  {
    VCard localVCard = new VCard();
    DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
    Document localDocument = localDocumentBuilder.parse(new ByteArrayInputStream(paramString.getBytes("UTF-8")));
    new VCardReader(localVCard, localDocument).initializeFields();
    return localVCard;
  }

  private static class VCardReader
  {
    private final VCard vCard;
    private final Document document;

    VCardReader(VCard paramVCard, Document paramDocument)
    {
      this.vCard = paramVCard;
      this.document = paramDocument;
    }

    public void initializeFields()
    {
      this.vCard.setFirstName(getTagContents("GIVEN"));
      this.vCard.setLastName(getTagContents("FAMILY"));
      this.vCard.setMiddleName(getTagContents("MIDDLE"));
      setupPhoto();
      setupEmails();
      this.vCard.setOrganization(getTagContents("ORGNAME"));
      this.vCard.setOrganizationUnit(getTagContents("ORGUNIT"));
      setupSimpleFields();
      setupPhones();
      setupAddresses();
    }

    private void setupPhoto()
    {
      Object localObject1 = null;
      Object localObject2 = null;
      NodeList localNodeList1 = this.document.getElementsByTagName("PHOTO");
      if (localNodeList1.getLength() != 1)
        return;
      Node localNode1 = localNodeList1.item(0);
      NodeList localNodeList2 = localNode1.getChildNodes();
      int i = localNodeList2.getLength();
      ArrayList localArrayList = new ArrayList(i);
      for (int j = 0; j < i; j++)
        localArrayList.add(localNodeList2.item(j));
      String str1 = null;
      String str2 = null;
      Iterator localIterator = localArrayList.iterator();
      while (localIterator.hasNext())
      {
        Node localNode2 = (Node)localIterator.next();
        str1 = localNode2.getNodeName();
        str2 = localNode2.getTextContent();
        if (str1.equals("BINVAL"))
          localObject1 = str2;
        else if (str1.equals("TYPE"))
          localObject2 = str2;
      }
      if ((localObject1 == null) || (localObject2 == null))
        return;
      this.vCard.setAvatar(localObject1, localObject2);
    }

    private void setupEmails()
    {
      NodeList localNodeList = this.document.getElementsByTagName("USERID");
      if (localNodeList == null)
        return;
      for (int i = 0; i < localNodeList.getLength(); i++)
      {
        Element localElement = (Element)localNodeList.item(i);
        if ("WORK".equals(localElement.getParentNode().getFirstChild().getNodeName()))
          this.vCard.setEmailWork(getTextContent(localElement));
        else
          this.vCard.setEmailHome(getTextContent(localElement));
      }
    }

    private void setupPhones()
    {
      NodeList localNodeList1 = this.document.getElementsByTagName("TEL");
      if (localNodeList1 == null)
        return;
      for (int i = 0; i < localNodeList1.getLength(); i++)
      {
        NodeList localNodeList2 = localNodeList1.item(i).getChildNodes();
        Object localObject1 = null;
        Object localObject2 = null;
        String str1 = null;
        for (int j = 0; j < localNodeList2.getLength(); j++)
        {
          Node localNode = localNodeList2.item(j);
          if (localNode.getNodeType() == 1)
          {
            String str2 = localNode.getNodeName();
            if ("NUMBER".equals(str2))
              str1 = getTextContent(localNode);
            else if (isWorkHome(str2))
              localObject1 = str2;
            else
              localObject2 = str2;
          }
        }
        if ((localObject2 != null) && (str1 != null))
          if ("HOME".equals(localObject1))
            this.vCard.setPhoneHome(localObject2, str1);
          else
            this.vCard.setPhoneWork(localObject2, str1);
      }
    }

    private boolean isWorkHome(String paramString)
    {
      return ("HOME".equals(paramString)) || ("WORK".equals(paramString));
    }

    private void setupAddresses()
    {
      NodeList localNodeList1 = this.document.getElementsByTagName("ADR");
      if (localNodeList1 == null)
        return;
      for (int i = 0; i < localNodeList1.getLength(); i++)
      {
        Element localElement = (Element)localNodeList1.item(i);
        Object localObject = null;
        ArrayList localArrayList1 = new ArrayList();
        ArrayList localArrayList2 = new ArrayList();
        NodeList localNodeList2 = localElement.getChildNodes();
        for (int j = 0; j < localNodeList2.getLength(); j++)
        {
          Node localNode = localNodeList2.item(j);
          if (localNode.getNodeType() == 1)
          {
            String str = localNode.getNodeName();
            if (isWorkHome(str))
            {
              localObject = str;
            }
            else
            {
              localArrayList1.add(str);
              localArrayList2.add(getTextContent(localNode));
            }
          }
        }
        for (j = 0; j < localArrayList2.size(); j++)
          if ("HOME".equals(localObject))
            this.vCard.setAddressFieldHome((String)localArrayList1.get(j), (String)localArrayList2.get(j));
          else
            this.vCard.setAddressFieldWork((String)localArrayList1.get(j), (String)localArrayList2.get(j));
      }
    }

    private String getTagContents(String paramString)
    {
      NodeList localNodeList = this.document.getElementsByTagName(paramString);
      if ((localNodeList != null) && (localNodeList.getLength() == 1))
        return getTextContent(localNodeList.item(0));
      return null;
    }

    private void setupSimpleFields()
    {
      NodeList localNodeList = this.document.getDocumentElement().getChildNodes();
      for (int i = 0; i < localNodeList.getLength(); i++)
      {
        Node localNode = localNodeList.item(i);
        if ((localNode instanceof Element))
        {
          Element localElement = (Element)localNode;
          String str = localElement.getNodeName();
          if (localElement.getChildNodes().getLength() == 0)
            this.vCard.setField(str, "");
          else if ((localElement.getChildNodes().getLength() == 1) && ((localElement.getChildNodes().item(0) instanceof Text)))
            this.vCard.setField(str, getTextContent(localElement));
        }
      }
    }

    private String getTextContent(Node paramNode)
    {
      StringBuilder localStringBuilder = new StringBuilder();
      appendText(localStringBuilder, paramNode);
      return localStringBuilder.toString();
    }

    private void appendText(StringBuilder paramStringBuilder, Node paramNode)
    {
      NodeList localNodeList = paramNode.getChildNodes();
      for (int i = 0; i < localNodeList.getLength(); i++)
      {
        Node localNode = localNodeList.item(i);
        String str = localNode.getNodeValue();
        if (str != null)
          paramStringBuilder.append(str);
        appendText(paramStringBuilder, localNode);
      }
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.provider.VCardProvider
 * JD-Core Version:    0.6.1
 */